package net.sureon.common.config;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import net.sureon.common.I18nSupport;

import org.dom4j.Attribute;
import org.dom4j.Element;

public class MailConfig {

	private static final String CONFIG_SMTP_HOST = "smtpHost";
	private static final String CONFIG_SMTP_AUTH = "auth";
	private static final String CONFIG_SMTP_USERNAME = "userName";
	private static final String CONFIG_SMTP_PASSWORD = "password";
	private static final String CONFIG_SMTP_TP_SIZE = "threadPoolSize";
	private static final String CONFIG_FROM_NAME = "fromName";
	private static final String CONFIG_FROM = "from";

	private String smtpHost;
	private String auth;
	private String userName;
	private String password;
	private String fromName;
	private String from;
	private int threadPoolSize;
	private EmailTemplates emailTemplates;

	public MailConfig(Element mailElem) {
		smtpHost = mailElem.elementText(CONFIG_SMTP_HOST);
		auth = mailElem.elementText(CONFIG_SMTP_AUTH);
		userName = mailElem.elementText(CONFIG_SMTP_USERNAME);
		password = mailElem.elementText(CONFIG_SMTP_PASSWORD);
		fromName = mailElem.elementText(CONFIG_FROM_NAME);
		from = mailElem.elementText(CONFIG_FROM);
		threadPoolSize = Integer.parseInt(mailElem
				.elementText(CONFIG_SMTP_TP_SIZE));
		emailTemplates = new EmailTemplates(mailElem.element("emailTemplates").elements());
	}

	/**
	 * @return the smtpHost
	 */
	public String getSmtpHost() {
		return smtpHost;
	}

	/**
	 * @return the auth
	 */
	public String getAuth() {
		return auth;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @return the threadPoolSize
	 */
	public int getThreadPoolSize() {
		return threadPoolSize;
	}

	/**
	 * @return the fromName
	 */
	public String getFromName() {
		return fromName;
	}

	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}

	public String getTemplate(String templateName) {
		return getTemplate(templateName, null);
	}

	public String getTemplate(String templateName, Locale locale) {
		Locale theLocale = locale;
		if (locale == null) {
			theLocale = I18nSupport.defaultLocale;
		}
		return emailTemplates.getTemplate(templateName, theLocale);
	}

	public String getSubject(String templateName) {
		return getSubject(templateName, null);
	}

	public String getSubject(String templateName, Locale locale) {
		Locale theLocale = locale;
		if (locale == null) {
			theLocale = I18nSupport.defaultLocale;
		}
		return emailTemplates.getSubject(templateName, theLocale);
	}

	public static final String DAILY_TIMING_NOT_FILLED = "not_filled_daily_timing";
	public static final String DAILY_TIMING_NOT_FILLED_TITLE = "Reminder: xxxx yyyy reminder.";

	private static class EmailTemplates {
		private Map<String, String> templateTxts = new HashMap<String, String>();
		private Map<String, String> templateSubjects = new HashMap<String, String>();

		private EmailTemplates(List templates) {
			Element e;
			Attribute localeAttr, nameAttr, subjectAttr;
			for (Object o : templates) {
				if (o instanceof Element) {
					e = (Element) o;
					localeAttr = e.attribute("locale");
					nameAttr = e.attribute("name");
					subjectAttr = e.attribute("subject");

					put(templateTxts, nameAttr.getValue(), new Locale(
							localeAttr.getValue()), e.getText());

					put(templateSubjects, nameAttr.getValue(), new Locale(
							localeAttr.getValue()), subjectAttr.getValue());
				}
			}
		}

		private void put(Map<String, String> map, String name, Locale locale,
				String template) {
			map.put(generateKey(name, locale), template);
		}

		private String getTemplate(String name, Locale locale) {
			return templateTxts.get(generateKey(name, locale));
		}

		private String getSubject(String name, Locale locale) {
			return templateSubjects.get(generateKey(name, locale));
		}

		private String generateKey(String name, Locale locale) {
			return name + "-" + locale.toString().toLowerCase();
		}
	}

}
